package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.services.*;
import cn.yunhe.util.ListToXml;
import cn.yunhe.util.StringToMap;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xiaoxin on 2018/5/23.
 */
@RestController
@RequestMapping("/paper")
public class PaperController{


    @Resource
    private IPaperServices iPaperServices;

    @Resource
    private ILibServices libServices;

    @Resource
    private IPointServices pointServices;

    @Resource
    private ITypeServices typeServices;

    @Resource
    private IPtypeServices ptypeServices;

    @Resource
    private IClassServices classServices;

    @Resource
    private IProblemServices problemServices;

    ModelAndView mv=new ModelAndView();

    @RequestMapping("/addPaper")
    public String addPaper(@RequestParam(value = "ids[]",required = false)  Integer[]  ids,
                           @RequestParam(value = "daos[]") Integer[] daos,
                           @RequestParam(value = "fens[]") Integer[] fens,
                           @RequestParam(value = "options[]") Integer[] options,
                           @RequestParam(value = "t_name[]") String[] type_name,
                           String paper_name,Integer ptype_id,Integer class_id,
                           String pwd,String full,String pass,Integer dura
                           ){

        List<Integer> id = new ArrayList<Integer>();
        List<Integer> dao = new ArrayList<Integer>();
        List<Integer> fen = new ArrayList<Integer>();
        List<Integer> points = Arrays.asList(options);
        List<String> t_name = new ArrayList<String>();
        int ae =0;
        for (int i = 0;i<Arrays.asList(daos).size();i++){
            if (Arrays.asList(daos).get(i)!=null){
                id.add(ae,Arrays.asList(ids).get(i));
                dao.add(ae,Arrays.asList(daos).get(i));
                fen.add(ae,Arrays.asList(fens).get(i));
                t_name.add(ae,Arrays.asList(type_name).get(i));
                ae++;
            }else{
                continue;
            }

        }
        Integer fu = Integer.valueOf(full);
        Integer ps = Integer.valueOf(pass);
        Integer dr = Integer.valueOf(dura);
        System.out.println(points.get(0));
        String ttype=" ";
        //讲题型和分值拼接成字符串供后台使用
        for (int i = 0;i<id.size();i++ ){
            if (fen.get(i)!=null){
                ttype += t_name.get(i).toString()+"-"+fen.get(i).toString()+",";
            }else{
                continue;
            }
        }
        AddPaPer addPaPer = new AddPaPer(class_id,dao,fen,id,paper_name,points,ptype_id,pwd,ttype,fu,ps,dr);
        int a = iPaperServices.addPaper(addPaPer);
        if (a>0) {
            return "redirect:/paper/listPaper.do";
        }else{
            return "success";
        }

    }

    @RequestMapping("/showAddPaPer")
    public ModelAndView showAddPaPer(){
        ModelAndView mo = new ModelAndView();
        //传入老师session里面的id值
        mo.addObject("classList",classServices.getListClass(1));
        mo.addObject("ptypeList",ptypeServices.getListPtype());
        mo.addObject("typeList",typeServices.getTypeList());
        mo.addObject("libList",libServices.getListLib());
        mo.setViewName("/paper/addPePer");
        return mo;
    }

    @RequestMapping("/sdonAddPaper")
    public String sdonAddPaper(Paper paper){
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        paper.setCreat_time(date);
        Teacher teacher = new Teacher();
        teacher.setTeacher_id(1);
        paper.setTeacher(teacher);
        int a = iPaperServices.saddPaper(paper);
        System.out.println(a);
        return null;
    }


    @RequestMapping("/showPoint")
    public List<Point> showPoint(int id){
        return pointServices.getLibPoint(id);
    }


    @RequestMapping("/zaddPaper")
    public int addPaper(Paper paper){
        return iPaperServices.addPaper(null);
    }


    @RequestMapping("/listPaper")
    public ModelAndView listPaper(){
        ModelAndView mo = new ModelAndView();
        //传入老师session里面的id值
        mo.addObject("classList",classServices.getListClass(1));
        mo.addObject("ptypeList",ptypeServices.getListPtype());
        mo.addObject("typeList",typeServices.getTypeList());
        mo.addObject("libList",libServices.getListLib());
        mo.setViewName("/paper/listPaper");
        return mo;
    }

    @RequestMapping("/getListPapers")
    public Map getListPapers(int pageNum, int pageSize , Paper paper){
        Map map = new HashMap();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("Paper",paper);
        Page page = iPaperServices.getListPapers(map);
        map.put("page",page);
        map.put("totalPages", page.getPages());
        return map;
    }

    @RequestMapping("/delePaper")
    public int delePaper(int paper_id){
        return iPaperServices.delePaper(paper_id);
    }
    @RequestMapping("/editOnline")
    public int editOnline(Paper paper){
        return iPaperServices.editOnline(paper);
    }
    @RequestMapping("/findpa")
    public ModelAndView findpa(Paper paper){
        ModelAndView mv = new ModelAndView();
        mv.addObject("ptypeList",ptypeServices.getListPtype());
        mv.addObject("paper",iPaperServices.showOnePaperInfo(paper));
        mv.setViewName("/paper/editPaper");
        mv.addObject("classList",classServices.getListClass(1));
        return mv;
    }
    @RequestMapping("/showPeper")
    public ModelAndView showPeper(Integer paper_id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/paper/showPaper");
        mv.addObject("paper_id",paper_id);
        return mv;
    }

    @RequestMapping("/editPaper")
    public int editPaper(Paper paper){
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        paper.setCreat_time(date);
        Teacher teacher= new Teacher();
        teacher.setTeacher_id(1);
        paper.setTeacher(teacher);
        return iPaperServices.editPaper(paper);
    }

    @RequestMapping("/loadPaper")
    public Map<String,Object> loadPaper(Integer paper_id){
        ListToXml lt = new ListToXml();
        //获取试卷
        Paper paper = iPaperServices.showOnePaper(paper_id);
        //获取当前试卷试题集合
        List<StudentProblem> list = lt.xmlToList(paper.getContent());
        //筛选试题类型 并分类
        //单选集合
        List<StudentProblem> onlyList = new ArrayList<>();
        //多选集合
        List<StudentProblem> manyList = new ArrayList<>();
        //单选集合
        List<StudentProblem> judgeList = new ArrayList<>();
        for (StudentProblem p : list){
            //单选题判断
            if(p.getType().equals("单选题")){
                onlyList.add(p);
            }
            //多选题判断
            if(p.getType().equals("多选题")){
                manyList.add(p);
            }
            //判断题判断
            if(p.getType().equals("判断题")){
                judgeList.add(p);
            }
        }
        //试卷类型分值
        String typeScoreStr = paper.getStruc();

        Map<String,Object> map = new HashMap<>();
        map.put("paper",paper);
        map.put("onlyList",onlyList);
        map.put("manyList",manyList);
        map.put("judgeList",judgeList);
        System.out.println("onlyList====="+onlyList.size());
        System.out.println("manyList====="+manyList.size());
        System.out.println("judgeList====="+judgeList.size());
        return map;
    }

    @RequestMapping("/sAddPaPer")
    public int adAddPaPer(@RequestBody DAddPaPer dAddPaPer){
        Paper paper = new Paper();
        paper.setPaper_id(Integer.parseInt(dAddPaPer.getPaper().get("paper_id").toString()));
        paper.setContent(ListToXml.listToXml(dAddPaPer.getCon().get("cont")));
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        paper.setCreat_time(date);
        paper.setPaper_name(dAddPaPer.getPaper().get("paper_name").toString());
        paper.setPass(Integer.parseInt(dAddPaPer.getPaper().get("pass").toString()));
        paper.setFull(Integer.parseInt(dAddPaPer.getPaper().get("full").toString()));
        paper.setDura(dAddPaPer.getPaper().get("dura").toString());
        Teacher teacher = new Teacher();
        teacher.setTeacher_id(1);
        paper.setTeacher(teacher);
        String ttype="";
        //讲题型和分值拼接成字符串供后台使用
        for (Map.Entry<String, Object> entry : dAddPaPer.getType().entrySet()) {
            if (entry.getKey().equals("only") && !entry.getValue().equals("0")){
                ttype+="单选题-"+entry.getValue()+",";
            }
            if (entry.getKey().equals("many") && !entry.getValue().equals("0")){
                ttype+="多选题-"+entry.getValue()+",";
            }
            if (entry.getKey().equals("judge") && !entry.getValue().equals("0")){
                ttype+="判断题-"+entry.getValue()+",";
            }
        }
        paper.setStruc(ttype);
        iPaperServices.sdEditPaper(paper);
        return 0;
    }
    @RequestMapping("/toAddproblem")
    public ModelAndView toAddProblme(){
        List<Lib> listLib=libServices.findLib();
        mv.addObject("listLib" ,listLib);
        List<Type> listType=typeServices.findType();
        mv.addObject("listType" ,listType);
        mv.setViewName("/paper/addProblem");
        return mv;

    }
    @RequestMapping("/adAddpro")
    public Map<String,Object> adAddpro(@RequestParam(value = "adAdd[]") Integer[] adAdd){
        List<Integer> adAdds = new ArrayList<Integer>();
        adAdds = Arrays.asList(adAdd);
        List<StudentProblem> list = new ArrayList<StudentProblem>();
        for (Integer sd :adAdds){
            Problem problem =  problemServices.findProblem(sd);
                //选项
                problem.setPro_mapname(StringToMap.stringToMap( problem.getPro_choice()));
                //图片
                problem.setPro_img(StringToMap.imgToMap(problem.getPro_choice()));

            StudentProblem studentProblem = new StudentProblem();
            //考题id
            studentProblem.setPid(problem.getPro_id());
            //正确答案
            studentProblem.setCorrectAnswer(problem.getP_true());
            //等级
            studentProblem.setGrade(problem.getGrade());
            System.out.println(problem.getPro_mapname());
            //选项
            studentProblem.setPchoice(problem.getPro_mapname());
            //图片
            studentProblem.setImages((LinkedHashMap<String, String>) problem.getPro_img());
            //题目名称
            studentProblem.setPname(problem.getPro_name());
            //学生答案
            studentProblem.setStudentAnswer(null);
            //考题类型
            studentProblem.setType(problem.getType().getType_name());
            //解析
            studentProblem.setResolve(problem.getTrue_res());
            list.add(studentProblem);
        }
        List<StudentProblem> onlyList = new ArrayList<>();
        //多选集合
        List<StudentProblem> manyList = new ArrayList<>();
        //单选集合
        List<StudentProblem> judgeList = new ArrayList<>();
        for (StudentProblem p : list){
            //单选题判断
            if(p.getType().equals("单选题")){
                onlyList.add(p);
            }
            //多选题判断
            if(p.getType().equals("多选题")){
                manyList.add(p);
            }
            //判断题判断
            if(p.getType().equals("判断题")){
                judgeList.add(p);
            }
        }
        Map map = new HashMap<>();
        map.put("onlyList",onlyList);
        map.put("manyList",manyList);
        map.put("judgeList",judgeList);
        ModelAndView mv = new ModelAndView();
        return map;
    }


    public IClassServices getClassServices() {
        return classServices;
    }

    public void setClassServices(IClassServices classServices) {
        this.classServices = classServices;
    }

    public IPaperServices getiPaperServices() {
        return iPaperServices;
    }

    public void setiPaperServices(IPaperServices iPaperServices) {
        this.iPaperServices = iPaperServices;
    }

    public ILibServices getLibServices() {
        return libServices;
    }

    public void setLibServices(ILibServices libServices) {
        this.libServices = libServices;
    }

    public IPointServices getPointServices() {
        return pointServices;
    }

    public void setPointServices(IPointServices pointServices) {
        this.pointServices = pointServices;
    }

    public IPtypeServices getPtypeServices() {
        return ptypeServices;
    }

    public void setPtypeServices(IPtypeServices ptypeServices) {
        this.ptypeServices = ptypeServices;
    }

    public ITypeServices getTypeServices() {
        return typeServices;
    }

    public void setTypeServices(ITypeServices typeServices) {
        this.typeServices = typeServices;
    }
}
